package com.shrcl.engineerprocess.domain;

import com.google.common.collect.Iterables;
import com.shrcl.engineerprocess.domain.CostEstimationDAO;
import com.shrcl.engineerprocess.domain.Engineer;
import com.shrcl.engineerprocess.domain.EngineerDAO;
import com.shrcl.engineerprocess.domain.EngineerLevel;
import com.shrcl.engineerprocess.domain.EngineerLevelDAO;
import com.shrcl.engineerprocess.domain.EngineerLevelSalary;
import com.shrcl.engineerprocess.domain.EngineerLevelSalaryDAO;
import com.shrcl.engineerprocess.domain.EngineerMonthlyCost;
import com.shrcl.engineerprocess.domain.EngineerProcessTaskExtension;
import com.shrcl.engineerprocess.domain.MonthlyLevelCost;
import com.shrcl.engineerprocess.domain.organization.EngineerTechnicalLeaderRoleProvider;
import com.shrct.businessprocess.organization.RCOrganizationService;
import edu.fudan.langlab.domain.security.ISystemUserService;
import edu.fudan.langlab.domain.task.TSKTask;
import edu.fudan.langlab.domain.task.TSKTaskCriteria;
import edu.fudan.langlab.domain.task.TSKTaskExtension;
import edu.fudan.langlab.domain.task.TSKTaskOwner;
import edu.fudan.langlab.domain.workflow.ProcessManager;
import edu.fudan.mylang.pf.IGenericQuery;
import edu.fudan.mylang.pf.ISystemDateTimeService;
import edu.fudan.mylang.pf.test.BaseTest;
import edu.fudan.mylang.utils.DateUtil;
import edu.fudan.mylang.utils.DateUtil.Month;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import mock.ISystemDateTimeMocker;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.InputOutput;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(locations = "classpath:HibernateApplicationContext-test.xml")
public class EngineerMonthlyCostTest extends BaseTest {
  public EngineerMonthlyCostTest() {
    super();
  }
  
  @Autowired
  private RCOrganizationService rcOrganizationService;
  
  @Autowired
  private EngineerTechnicalLeaderRoleProvider engineerTechnicalLeaderRoleProvider;
  
  @Autowired
  private ISystemUserService systemUserService;
  
  @Autowired
  private EngineerDAO engineerDAO;
  
  @Autowired
  private CostEstimationDAO costEstimationDAO;
  
  @Autowired
  private ProcessManager processManager;
  
  @Autowired
  private ISystemDateTimeService systemDateTimeService;
  
  @Autowired
  private ISystemDateTimeMocker systemDateTimeMocker;
  
  @Autowired
  private EngineerLevelDAO engineerLevelDAO;
  
  @Autowired
  private EngineerLevelSalaryDAO engineerLevelSalaryDAO;
  
  public EngineerProcessTaskExtension getEngineerProcessTaskExtension4(final TSKTaskOwner owner) {TSKTaskCriteria tSKTaskCriteria=getObjectFactory().createQueryCriteria(TSKTaskCriteria.class);
    
    TSKTaskCriteria _setOwner = tSKTaskCriteria.setOwner(owner);
    Collection<TSKTask> _list = _setOwner.list();
    final Function1<TSKTask,Boolean> _function = new Function1<TSKTask,Boolean>() {
        public Boolean apply(final TSKTask it) {
          Boolean _isFinished = it.isFinished();
          boolean _not = (!_isFinished);
          return Boolean.valueOf(_not);
        }
      };
    Iterable<TSKTask> _filter = IterableExtensions.<TSKTask>filter(_list, _function);
    final Function1<TSKTask,TSKTaskExtension> _function_1 = new Function1<TSKTask,TSKTaskExtension>() {
        public TSKTaskExtension apply(final TSKTask it) {
          TSKTaskExtension _extension = it.getExtension();
          return _extension;
        }
      };
    Iterable<TSKTaskExtension> _map = IterableExtensions.<TSKTask, TSKTaskExtension>map(_filter, _function_1);
    Iterable<EngineerProcessTaskExtension> _filter_1 = Iterables.<EngineerProcessTaskExtension>filter(_map, EngineerProcessTaskExtension.class);
    EngineerProcessTaskExtension _head = IterableExtensions.<EngineerProcessTaskExtension>head(_filter_1);
    return _head;
  }
  
  @Test
  public void initProcess() {
    InputOutput.<String>println("====>start EngineerMonthlyCostTest======");
    final Procedure1<EngineerLevel> _function = new Procedure1<EngineerLevel>() {
        public void apply(final EngineerLevel it) {
          it.setLevelId("001");
          it.setName("\u9879\u76EE\u7ECF\u7406");
        }
      };
    final EngineerLevel level1 = this.engineerLevelDAO.create(_function);
    final Procedure1<EngineerLevel> _function_1 = new Procedure1<EngineerLevel>() {
        public void apply(final EngineerLevel it) {
          it.setLevelId("002");
          it.setName("\u5DE5\u7A0B\u5E08");
        }
      };
    final EngineerLevel level2 = this.engineerLevelDAO.create(_function_1);
    final Procedure1<Engineer> _function_2 = new Procedure1<Engineer>() {
        public void apply(final Engineer it) {
          it.setName("Rock");
        }
      };
    final Engineer eng1 = this.engineerDAO.create(_function_2);
    final Procedure1<Engineer> _function_3 = new Procedure1<Engineer>() {
        public void apply(final Engineer it) {
          it.setName("Paul");
        }
      };
    final Engineer eng2 = this.engineerDAO.create(_function_3);
    eng1.addtoLevels(level1);
    eng1.addtoLevels(level2);
    eng2.addtoLevels(level2);
    final Procedure1<EngineerMonthlyCost> _function_4 = new Procedure1<EngineerMonthlyCost>() {
        public void apply(final EngineerMonthlyCost it) {
          Date _day = EngineerMonthlyCostTest.this.systemDateTimeService.today();
          it.setMonth(_day);
          it.setCost(Double.valueOf(8000d));
        }
      };
    final EngineerMonthlyCost emc1 = eng1.addMonthlyCost(_function_4);
    final Procedure1<EngineerMonthlyCost> _function_5 = new Procedure1<EngineerMonthlyCost>() {
        public void apply(final EngineerMonthlyCost it) {
          Date _day = EngineerMonthlyCostTest.this.systemDateTimeService.today();
          it.setMonth(_day);
          it.setCost(Double.valueOf(5000d));
        }
      };
    final EngineerMonthlyCost emc2 = eng2.addMonthlyCost(_function_5);IGenericQuery<EngineerLevelSalary> query=getObjectFactory().createGenericQuery(EngineerLevelSalary.class,"select e from com.shrcl.engineerprocess.domain.impl.EngineerLevelSalaryImpl as e   where  e.active = 1      ");
    final Collection<EngineerLevelSalary> salaryByLevel = query.list();
    int _size = salaryByLevel.size();
    String _plus = ("salaryByLevel.size=" + Integer.valueOf(_size));
    InputOutput.<String>println(_plus);
    EngineerLevelSalary _engineerLevelSalaryByLevel = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level1);
    Double _latestAvgCost = _engineerLevelSalaryByLevel.getLatestAvgCost();
    String _plus_1 = ("engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level1).getLatestAvgCost()" + _latestAvgCost);
    InputOutput.<String>println(_plus_1);
    EngineerLevelSalary _engineerLevelSalaryByLevel_1 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level1);
    Double _latestAvgCost_1 = _engineerLevelSalaryByLevel_1.getLatestAvgCost();
    boolean _equals = ((_latestAvgCost_1).doubleValue() == 8000d);
    Assert.assertTrue(_equals);
    EngineerLevelSalary _engineerLevelSalaryByLevel_2 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2);
    Double _latestAvgCost_2 = _engineerLevelSalaryByLevel_2.getLatestAvgCost();
    boolean _equals_1 = ((_latestAvgCost_2).doubleValue() == 6500d);
    Assert.assertTrue(_equals_1);
    EngineerMonthlyCost _currentMonthlyCost = eng1.getCurrentMonthlyCost();
    Assert.assertNotNull(_currentMonthlyCost);
    EngineerMonthlyCost _currentMonthlyCost_1 = eng1.getCurrentMonthlyCost();
    final Procedure1<EngineerMonthlyCost> _function_6 = new Procedure1<EngineerMonthlyCost>() {
        public void apply(final EngineerMonthlyCost it) {
          it.setCost(Double.valueOf(9000d));
        }
      };
    _currentMonthlyCost_1.updateCost(_function_6);
    EngineerLevelSalary _engineerLevelSalaryByLevel_3 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level1);
    Double _latestAvgCost_3 = _engineerLevelSalaryByLevel_3.getLatestAvgCost();
    boolean _equals_2 = ((_latestAvgCost_3).doubleValue() == 9000d);
    Assert.assertTrue(_equals_2);
    final EngineerLevelSalary mlc = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2);
    EngineerLevel _level = mlc.getLevel();
    String _name = _level.getName();
    String _plus_2 = ("engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2).level=" + _name);
    InputOutput.<String>println(_plus_2);
    Collection<MonthlyLevelCost> _monthlyLevelCost = mlc.getMonthlyLevelCost();
    int _size_1 = _monthlyLevelCost.size();
    String _plus_3 = ("engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2).monthlyLevelCost.size=" + Integer.valueOf(_size_1));
    InputOutput.<String>println(_plus_3);
    Collection<MonthlyLevelCost> _monthlyLevelCost_1 = mlc.getMonthlyLevelCost();
    final Procedure1<MonthlyLevelCost> _function_7 = new Procedure1<MonthlyLevelCost>() {
        public void apply(final MonthlyLevelCost item) {
          Date _month = item.getMonth();
          String _plus = ("item.month=" + _month);
          InputOutput.<String>println(_plus);
        }
      };
    IterableExtensions.<MonthlyLevelCost>forEach(_monthlyLevelCost_1, _function_7);
    EngineerLevelSalary _engineerLevelSalaryByLevel_4 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2);
    Double _latestAvgCost_4 = _engineerLevelSalaryByLevel_4.getLatestAvgCost();
    String _plus_4 = ("engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2).getLatestAvgCost()=" + _latestAvgCost_4);
    InputOutput.<String>println(_plus_4);
    EngineerLevelSalary _engineerLevelSalaryByLevel_5 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2);
    Double _latestAvgCost_5 = _engineerLevelSalaryByLevel_5.getLatestAvgCost();
    boolean _equals_3 = ((_latestAvgCost_5).doubleValue() == 7000d);
    Assert.assertTrue(_equals_3);
    Date _day = this.systemDateTimeService.today();
    Date _addMonths = DateUtil.addMonths(_day, 1);
    Date _trim2Month = DateUtil.trim2Month(_addMonths);
    this.systemDateTimeMocker.setCurrent(_trim2Month);
    eng1.monthlyCostTimer();
    eng2.monthlyCostTimer();
    EngineerMonthlyCost _currentMonthlyCost_2 = eng1.getCurrentMonthlyCost();
    final Procedure1<EngineerMonthlyCost> _function_8 = new Procedure1<EngineerMonthlyCost>() {
        public void apply(final EngineerMonthlyCost it) {
          it.setCost(Double.valueOf(10000d));
        }
      };
    _currentMonthlyCost_2.updateCost(_function_8);
    EngineerLevelSalary _engineerLevelSalaryByLevel_6 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level1);
    Double _latestAvgCost_6 = _engineerLevelSalaryByLevel_6.getLatestAvgCost();
    boolean _equals_4 = ((_latestAvgCost_6).doubleValue() == 10000d);
    Assert.assertTrue(_equals_4);
    EngineerLevelSalary _engineerLevelSalaryByLevel_7 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2);
    Double _latestAvgCost_7 = _engineerLevelSalaryByLevel_7.getLatestAvgCost();
    boolean _equals_5 = ((_latestAvgCost_7).doubleValue() == 7500d);
    Assert.assertTrue(_equals_5);
    EngineerLevelSalary _engineerLevelSalaryByLevel_8 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2);
    Collection<MonthlyLevelCost> _monthlyLevelCost_2 = _engineerLevelSalaryByLevel_8.getMonthlyLevelCost();
    final Function1<MonthlyLevelCost,Date> _function_9 = new Function1<MonthlyLevelCost,Date>() {
        public Date apply(final MonthlyLevelCost it) {
          Date _month = it.getMonth();
          Date _trim2Month = DateUtil.trim2Month(_month);
          return _trim2Month;
        }
      };
    List<MonthlyLevelCost> _sortBy = IterableExtensions.<MonthlyLevelCost, Date>sortBy(_monthlyLevelCost_2, _function_9);
    List<MonthlyLevelCost> _reverse = ListExtensions.<MonthlyLevelCost>reverse(_sortBy);
    final Function1<MonthlyLevelCost,Boolean> _function_10 = new Function1<MonthlyLevelCost,Boolean>() {
        public Boolean apply(final MonthlyLevelCost it) {
          return Boolean.valueOf(true);
        }
      };
    MonthlyLevelCost _findFirst = IterableExtensions.<MonthlyLevelCost>findFirst(_reverse, _function_10);
    final Procedure1<MonthlyLevelCost> _function_11 = new Procedure1<MonthlyLevelCost>() {
        public void apply(final MonthlyLevelCost it) {
          InputOutput.<String>println("================>Start");
          Date _month = it.getMonth();
          Month _month_1 = DateUtil.getMonth(_month);
          int _year = _month_1.getYear();
          InputOutput.<Integer>println(Integer.valueOf(_year));
          Date _month_2 = it.getMonth();
          Month _month_3 = DateUtil.getMonth(_month_2);
          int _month_4 = _month_3.getMonth();
          InputOutput.<Integer>println(Integer.valueOf(_month_4));
          InputOutput.<String>println("================>End");
          Double _avgCost = it.getAvgCost();
          String _plus = ("avgCost =" + _avgCost);
          InputOutput.<String>println(_plus);
          Double _avgCost_1 = it.getAvgCost();
          boolean _equals = ((_avgCost_1).doubleValue() == 7500d);
          Assert.assertTrue(_equals);
        }
      };
    ObjectExtensions.<MonthlyLevelCost>operator_doubleArrow(_findFirst, _function_11);
    EngineerLevelSalary _engineerLevelSalaryByLevel_9 = this.engineerLevelSalaryDAO.getEngineerLevelSalaryByLevel(level2);
    Collection<MonthlyLevelCost> _monthlyLevelCost_3 = _engineerLevelSalaryByLevel_9.getMonthlyLevelCost();
    final Function1<MonthlyLevelCost,Date> _function_12 = new Function1<MonthlyLevelCost,Date>() {
        public Date apply(final MonthlyLevelCost it) {
          Date _month = it.getMonth();
          Date _trim2Month = DateUtil.trim2Month(_month);
          return _trim2Month;
        }
      };
    List<MonthlyLevelCost> _sortBy_1 = IterableExtensions.<MonthlyLevelCost, Date>sortBy(_monthlyLevelCost_3, _function_12);
    List<MonthlyLevelCost> _reverse_1 = ListExtensions.<MonthlyLevelCost>reverse(_sortBy_1);
    final Function1<MonthlyLevelCost,Boolean> _function_13 = new Function1<MonthlyLevelCost,Boolean>() {
        public Boolean apply(final MonthlyLevelCost it) {
          return Boolean.valueOf(true);
        }
      };
    MonthlyLevelCost _findLast = IterableExtensions.<MonthlyLevelCost>findLast(_reverse_1, _function_13);
    final Procedure1<MonthlyLevelCost> _function_14 = new Procedure1<MonthlyLevelCost>() {
        public void apply(final MonthlyLevelCost it) {
          InputOutput.<String>println("================>Start");
          Date _month = it.getMonth();
          Month _month_1 = DateUtil.getMonth(_month);
          int _year = _month_1.getYear();
          InputOutput.<Integer>println(Integer.valueOf(_year));
          Date _month_2 = it.getMonth();
          Month _month_3 = DateUtil.getMonth(_month_2);
          int _month_4 = _month_3.getMonth();
          InputOutput.<Integer>println(Integer.valueOf(_month_4));
          InputOutput.<String>println("================>End");
          Double _avgCost = it.getAvgCost();
          String _plus = ("avgCost =" + _avgCost);
          InputOutput.<String>println(_plus);
          Double _avgCost_1 = it.getAvgCost();
          boolean _equals = ((_avgCost_1).doubleValue() == 7000d);
          Assert.assertTrue(_equals);
        }
      };
    ObjectExtensions.<MonthlyLevelCost>operator_doubleArrow(_findLast, _function_14);
  }
}
